package com.dimpt.cloud.api;

import com.dimpt.cloud.userauth.model.SessionSO;
import com.dimpt.cloud.userauth.model.UserSO;
import com.dimpt.cloud.userauth.service.IAuthService;
import com.dimpt.cloud.userauth.service.ISessionCacheService;
import com.dimpt.cloud.common.config.ResponseInfo;
import com.dimpt.cloud.common.exception.FailedException;
import com.dimpt.cloud.utils.IpAddress;
import com.dimpt.cloud.utils.ParseHttp;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Liujf
 * @Description: 授权管理
 * @Date: Created in 10:05 2018-05-08
 * @Version: 1.0
 */
@RestController
public class AuthController {

    @Autowired
    ISessionCacheService iSessionCacheService;

    @Autowired
    IAuthService iAuthService;

    /**
     * 用户登录
     *
     * @param username
     * @param password
     * @return map
     */
    @ApiOperation(value = "用户登录", notes = "用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名称", required = false, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "password", value = "密码", required = false, dataType = "String", paramType = "query"),
    })
    @GetMapping("/auth/username/{username}")
    public UserSO login(@PathVariable String username, @RequestParam("password") String password, HttpServletRequest request) {
        if (username == null)
            throw new FailedException(ResponseInfo.USERNAME_EMPTY.getErrorCode(), ResponseInfo.USERNAME_EMPTY.getErrorMsg());

        if (password == null)
            throw new FailedException(ResponseInfo.PASSWORD_EMPTY.getErrorCode(), ResponseInfo.PASSWORD_EMPTY.getErrorMsg());

        Map<String,String> loginMap = new HashMap<>();
        loginMap.put("userName",username);
        loginMap.put("password",password);
        loginMap.put("serverAddress",request.getServerName());
        loginMap.put("serverPort",String.valueOf(request.getServerPort()));
        loginMap.put("clientAddress", IpAddress.getIpAddr(request));
        loginMap.put("clientPort",String.valueOf(request.getRemotePort()));

        UserSO user =  iAuthService.login(loginMap);
        List list = new ArrayList<>();
        list.add(user);
        return user;
    }
    /**
     * 获取user信息
     * @return map
     */
    @ApiOperation(value = "获取user", notes = "获取user")
    @GetMapping("/auth")
    public List<UserSO> checkLogin(HttpServletRequest request) {
        String token = ParseHttp.getHttpAuthorization(request);
        SessionSO session = iSessionCacheService.getSession(token);
        UserSO userSO = iAuthService.checkUserIsLogin(session.getUserName());
        List list = new ArrayList<>();
        list.add(userSO);
        return list;
    }

    /**
     * 用户退出
     */
    @ApiOperation(value = "用户退出", notes = "用户退出")
    @DeleteMapping(value = "/auth")
    public void logout(HttpServletRequest request){

        String token = ParseHttp.getHttpAuthorization(request);
        iAuthService.logout(token);
    }
}
